import Goods from "../model/goods.model.js";
import { Op } from "sequelize";
export async function createClassGoods(data) {
  // 创建商品
  const res = await Goods.create(data);
  // console.log(res.toJSON());
  return res.dataValues;
}
export async function updateClassGoods(id, data) {
  // 修改商品
  const res = await Goods.update(data, { where: { id } });

  return res[0] > 0 ? true : false;
}

export async function getClassGoods(page, pageSize, name, inventory) {
  console.log(name, inventory);
  const offset = (page - 1) * pageSize; // 计算偏移量
  const where = {};
  if (name) {
    where.name = { [Op.like]: `%${name}%` };
  }
  if (inventory) {
    // 库存
    // 库存是精准搜索
    where.inventory = inventory;
    // where.inventory = { [Op.like]: `%${inventory}%` };
  }

  try {
    const res = await Goods.findAndCountAll({
      offset,
      limit: Number(pageSize),
      where,
    });
    return { list: res.rows, total: res.count };
  } catch (error) {
    return {
      list: [],
      total: 0,
    };
  }
}
export async function deleteClassGoods(id) {
  const res = await Goods.destroy({ where: { id } });
  return res > 0 ? true : false;
}
